﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.IO;

namespace PreProcessingLanguage
{
    class UploadData
    {
        private MySqlConnection connection;
        private string server;
        private int port;
        private string database;
        private string uid;
        private string password;

        public UploadData()
        {
            Initialize();
        }

        private void Initialize()
        {
            server = "localhost";
            port = 3306;
            database = "word";
            uid = "root";
            password = "";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "Port =" + port + ";" + "DATABASE=" +
            database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";CHARSET=utf8;";

            connection = new MySqlConnection(connectionString);

            try
            {
                connection.Open();
                Console.WriteLine("Connected");
            }
            catch (MySqlException ex)
            {
                //When handling errors, you can your application's response based 
                //on the error number.
                //The two most common error numbers when connecting are as follows:
                //0: Cannot connect to server.
                //1045: Invalid user name and/or password.
                switch (ex.Number)
                {
                    case 0:
                        Console.WriteLine("Cannot connect to server.  Contact administrator");
                        break;

                    case 1045:
                        Console.WriteLine("Invalid username/password, please try again");
                        break;
                }
            }
        }
        public bool readData(string path1, string path2, string path3)
        {
            try
            {
                StreamReader sr = new StreamReader(path1);
                Dictionary<string, double> pp;
                string str;
                int count=0;
                while ((str = sr.ReadLine()) != null)
                {
                    Console.WriteLine("Line word1:"+(count++));
                    string[] ppp = str.Split(':');
                    string query = "INSERT INTO word1 (word, value) VALUES('" + ppp[0] + "', '" + ppp[1] + "')";
                    MySqlCommand cmd = new MySqlCommand(query, connection);
                    //Execute command
                    cmd.ExecuteNonQuery();
                }
                sr.Close();
                sr = new StreamReader(path2);
                count = 0;
                while ((str = sr.ReadLine()) != null)
                {
                    Console.WriteLine("Line word2:" + (count++));
                    string[] ppp = str.Split(':');
                    string query = "INSERT INTO word2 (word, value) VALUES('" + ppp[0] + "', '" + ppp[1] + "')";
                    MySqlCommand cmd = new MySqlCommand(query, connection);
                    //Execute command
                    cmd.ExecuteNonQuery();
                }
                sr.Close();
                sr = new StreamReader(path3);
                count = 0;
                while ((str = sr.ReadLine()) != null)
                {
                    Console.WriteLine("Line word3:" + (count++));
                    string[] ppp = str.Split(':');
                    string query = "INSERT INTO word3 (word, value) VALUES('" + ppp[0] + "', '" + ppp[1] + "')";
                    MySqlCommand cmd = new MySqlCommand(query, connection);
                    //Execute command
                    cmd.ExecuteNonQuery();
                }
                sr.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}
